package org.zy.validate;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.session.SqlSession;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zy.bean.tresource;
import org.zy.bean.trole;
import org.zy.bean.tuser;
import org.zy.commons.SqlSessionTool;
import org.zy.dao.tresourceMapper;
import org.zy.dao.troleMapper;
import org.zy.dao.tuserMapper;

import com.alibaba.fastjson.JSON;

public class MyJdbcRealm extends AuthorizingRealm {
	final static Logger log = LoggerFactory.getLogger(MyJdbcRealm.class);
	static {
		log.info("装载自定义Ralm审查");
	}

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		log.info("角色权限审查开始");
		if (arg0 == null) {
			throw new AuthorizationException("参数方法为空");
		}
		// 获取当前登录用户名
		String username = (String) getAvailablePrincipal(arg0);
		log.info("核查用户=>" + username);

		// 打开Mybatis 获取一个session
		SqlSession session = SqlSessionTool.openSession();
		tuserMapper bean = session.getMapper(tuserMapper.class);

		// 从数据库获取当前用户所有角色,所有权限
		Set<String> setRoles = new HashSet<String>();
		Set<String> setResources = new HashSet<String>();

		// 迭代数据存入集合
		tuser user = bean.getUser_Roles_Resources_Info_ByUsername(username);
		
		List<trole> roles = user.getRoles();
		for (trole trole : roles) {
			// log.info("Role存入=>"+trole.getText());
			setRoles.add(trole.getText());
		}
		
		List<tresource> resources = user.getResource();
		for (tresource tresource : resources) {
			setResources.add(tresource.getTest());
		}
		
		log.info("Roles=>" + setRoles);
		log.info("Resources===>" + setResources);
		session.close();

		// 建立SimpleAuthorizationInfo存入角色和权限
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(setRoles);
		info.setStringPermissions(setResources);
		return info;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken arg0) throws AuthenticationException {
		log.info("登陆审查开始");
		// 获取token
		UsernamePasswordToken uptoken = (UsernamePasswordToken) arg0;
		String username = uptoken.getUsername();
		if (username == null)
			throw new AccountException("账号为空");

		// 打开Mybatis 获取一个session
		SqlSession session = SqlSessionTool.openSession();
		tuserMapper bean = session.getMapper(tuserMapper.class);

		// 从数据中获取密码
		tuser user = bean.getUserInfoByUsername(username);
		String pwd = user.getPwd();
		if (pwd == null) {
			throw new AccountException("密码为空错误");
		}

		// 封装AuthenticationInfo 返回给shiro核查
		AuthenticationInfo info = new SimpleAuthenticationInfo(username, pwd,
				getName());

		return info;

	}

	private Set<String> getPermissingSet(List<tresource> tresources) {
		Set<String> set = new HashSet<String>();
		for (tresource t : tresources) {
			set.add(t.getTest());
		}
		return set;
	}

	private Set<String> getRolesSet(List<trole> roles) {
		Set<String> set = new HashSet<String>();
		for (trole trole : roles) {
			set.add(trole.getText());
		}
		return set;
	}

}
